package cn.stylefeng.guns.sys.modular.dbServicelist.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.dbList.model.params.DblistParam;
import cn.stylefeng.guns.sys.modular.dbList.service.DblistService;
import cn.stylefeng.guns.sys.modular.dbServicelist.entity.DbServicelist;
import cn.stylefeng.guns.sys.modular.dbServicelist.entity.Exchange;
import cn.stylefeng.guns.sys.modular.dbServicelist.mapper.DbServicelistMapper;
import cn.stylefeng.guns.sys.modular.dbServicelist.model.params.DbServicelistParam;
import cn.stylefeng.guns.sys.modular.dbServicelist.model.result.DbServicelistResult;
import cn.stylefeng.guns.sys.modular.dbServicelist.service.DbServicelistService;
import cn.stylefeng.guns.sys.modular.dbcontentapproval.entity.DbContentapproval;
import cn.stylefeng.guns.sys.modular.dbcontentapproval.model.params.DbContentapprovalParam;
import cn.stylefeng.guns.sys.modular.dbcontentapproval.service.DbContentapprovalService;
import cn.stylefeng.guns.sys.modular.dbservicedetails.entity.DbServicedetails;
import cn.stylefeng.guns.sys.modular.dbservicedetails.model.params.DbServicedetailsParam;
import cn.stylefeng.guns.sys.modular.dbservicedetails.service.DbServicedetailsService;
import cn.stylefeng.guns.sys.modular.notice.model.params.DbNoticeParam;
import cn.stylefeng.guns.sys.modular.notice.model.result.DbNoticeResult;
import cn.stylefeng.guns.sys.modular.notice.service.DbNoticeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.models.auth.In;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.net.InetAddress;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 目录服务  服务实现类
 * </p>
 *
 * @author zhaoyiheng
 * @since 2020-08-19
 */
@Service
public class DbServicelistServiceImpl extends ServiceImpl<DbServicelistMapper, DbServicelist> implements DbServicelistService {
    @Autowired
    private DbContentapprovalService dbContentapprovalService;
    @Autowired
    private DbServicedetailsService dbServicedetailsService;
    @Autowired
    private DbNoticeService dbNoticeService;
    @Autowired
    private DblistService dblistService;

    @Override
    public Integer add(DbServicelistParam param, LoginUser user) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        Date parse = new Date();
        Calendar cd = Calendar.getInstance();
        cd.setTime(parse);
        //增加时间
        if ("3".equals(param.getEffectiveTime())) {
            cd.add(Calendar.MONTH, 3);

        } else if ("6".equals(param.getEffectiveTime())) {
            cd.add(Calendar.MONTH, 6);
        } else if ("9".equals(param.getEffectiveTime())) {
            cd.add(Calendar.YEAR, 1000);
        }
        DbServicelist entity = getEntity(param);
//        //同一个数据库不允许二次申请使用
//        DbServicelist dbName = baseMapper.selectOne(new QueryWrapper<DbServicelist>().eq("dbName", entity.getDbName()));
//        if (dbName != null) {
//            return 500;
//        }
        //数据库可以重复注册
        //通过数据库名字查询此数据库是否已经注册
        String jdbcUrl = "";
        jdbcUrl = "jdbc:mysql://" + param.getDbUrl() + ":" + param.getDbNumber() + "/" + param.getDbName() + "?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT";
        DbServicelistParam dbServicelistParam = new DbServicelistParam();
        dbServicelistParam.setDbName(param.getDbName());
        dbServicelistParam.setUserId(user.getId());
        List<DbServicelistResult> list = this.baseMapper.customList(dbServicelistParam);
        if (list.size() == 0) {
            entity.setEffectiveTime(simpleDateFormat.format(cd.getTime()));
            entity.setDbName(entity.getDbName());
            entity.setCreatedBy(user.getUsername());
            entity.setUserId(user.getId());
            entity.setSpState("0");
            entity.setState("0");
            entity.setJdbcUrl(jdbcUrl);
            entity.setOpen("0");
            entity.setCreatedTime(parse);
            entity.setUpdatedTime(parse);
            entity.setUpdatedBy(user.getUsername());
            this.save(entity);

            //保存一条通知
            DbNoticeParam dbNoticeParam = new DbNoticeParam();
            dbNoticeParam.setContent("注册申请");
            dbNoticeParam.setServiceId(entity.getId());
            dbNoticeParam.setDepartment(user.getDeptName());
            dbNoticeParam.setSpState("0");
            dbNoticeParam.setState("0");
            dbNoticeParam.setAdminState("0");
            dbNoticeParam.setFirstIn("0");
            dbNoticeParam.setServiceType(param.getServicetype());
            dbNoticeParam.setSystem(param.getSystem());
            dbNoticeParam.setXxContent(param.getDbName() + "数据库申请注册");
            dbNoticeParam.setUserId(String.valueOf(user.getId()));
            dbNoticeParam.setRemark(param.getStandard());
            dbNoticeService.add(dbNoticeParam);
            DbNoticeResult result = dbNoticeService.findByParam(dbNoticeParam);


            //保存到审核表
            DbContentapproval dbContentapproval = new DbContentapproval();
            BeanUtils.copyProperties(entity, dbContentapproval);
            dbContentapproval.setServiceid(entity.getId());
            dbContentapproval.setUserId(user.getId() + "");
            dbContentapproval.setContent("0");//0是注册申请
            dbContentapproval.setRemarks(entity.getRemarks());
            dbContentapproval.setNoticeId(result.getId());
            dbContentapproval.setState("0");//0是默认待处理
            dbContentapprovalService.save(dbContentapproval);
        } else if (list.get(0).getSpState().equals("2")) {
            //说明之前申请数据库被拒绝过,这次是重新注册,重新注册直接修改状态
//            DbServicelist dbServicelist = new DbServicelist();
            dbServicelistParam.setId(list.get(0).getId());
            dbServicelistParam.setDbName(list.get(0).getDbName());
            dbServicelistParam.setDepartment(param.getDepartment());
            dbServicelistParam.setUserId(user.getId());
            dbServicelistParam.setUsername(param.getUsername());
            dbServicelistParam.setPassword(param.getPassword());
            dbServicelistParam.setServicetype(param.getServicetype());
            dbServicelistParam.setSystem(param.getSystem());
            dbServicelistParam.setDetails(param.getDetails());
            dbServicelistParam.setStandard(param.getStandard());
            dbServicelistParam.setState("0");
            dbServicelistParam.setSpState("0");
            dbServicelistParam.setOpen("0");
            dbServicelistParam.setDbUrl(param.getDbUrl());
            dbServicelistParam.setExchangeDate(param.getExchangeDate());
            dbServicelistParam.setExchangeId(param.getExchangeId());
            dbServicelistParam.setDbType(param.getDbType());
            dbServicelistParam.setDbNumber(param.getDbNumber());
            dbServicelistParam.setDbMemory(param.getDbMemory());
            dbServicelistParam.setEffectiveTime(simpleDateFormat.format(cd.getTime()));
            dbServicelistParam.setRemarks(param.getRemarks());
            dbServicelistParam.setRevision(param.getRevision());
            dbServicelistParam.setCreatedBy(list.get(0).getCreatedBy());
            dbServicelistParam.setCreatedTime(list.get(0).getCreatedTime());
            dbServicelistParam.setUpdatedBy(user.getUsername());
            dbServicelistParam.setUpdatedTime(parse);
            //更新状态
            this.baseMapper.reApply(dbServicelistParam);


            //保存一条通知
            DbNoticeParam dbNoticeParam = new DbNoticeParam();
            dbNoticeParam.setContent("注册申请");
            dbNoticeParam.setServiceId(list.get(0).getId());
            dbNoticeParam.setDepartment(user.getDeptName());
            dbNoticeParam.setSpState("0");
            dbNoticeParam.setState("0");
            dbNoticeParam.setAdminState("0");
            dbNoticeParam.setFirstIn("0");
            dbNoticeParam.setServiceType(param.getServicetype());
            dbNoticeParam.setSystem(param.getSystem());
            dbNoticeParam.setXxContent(param.getDbName() + "数据库申请注册");
            dbNoticeParam.setUserId(String.valueOf(user.getId()));
            dbNoticeParam.setRemark(param.getStandard());
            dbNoticeService.add(dbNoticeParam);
            DbNoticeResult result = dbNoticeService.findByParam(dbNoticeParam);


            //保存到审核表
            DbContentapproval dbContentapproval = new DbContentapproval();
            BeanUtils.copyProperties(entity, dbContentapproval);
            dbContentapproval.setServiceid(list.get(0).getId());
            dbContentapproval.setUserId(user.getId() + "");
            dbContentapproval.setContent("0");//0是注册申请
            dbContentapproval.setRemarks(entity.getRemarks());
            dbContentapproval.setNoticeId(result.getId());
            dbContentapproval.setCreatedBy(list.get(0).getCreatedBy());
            dbContentapproval.setCreatedTime(list.get(0).getCreatedTime());
            dbContentapproval.setUpdatedBy(user.getUsername());
            dbContentapproval.setUpdatedTime(parse);
            dbContentapproval.setState("0");//0是默认待处理
            dbContentapprovalService.save(dbContentapproval);
        }


        return 200;
    }

    @Override
    public void delete(DbServicelistParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public Integer update(DbServicelistParam param) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        DbServicelist oldEntity = getOldEntity(param);
        DbServicelist newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdatedTime(new Date());
        newEntity.setUpdatedBy(LoginContextHolder.getContext().getUser().getUsername());
        if (param.getState().equals("1")) {
            //数据库废弃之后，管理端将数据库状态为异常
            DblistParam dblistParam = new DblistParam();
            dblistParam.setServiceListId(param.getId());
            dblistParam.setState("1");
            dblistService.alterDbListInfoByServiceId(dblistParam);
        }
        if (param.getState().equals("0")) {
            //数据库废弃之后，管理端将数据库状态为异常
            DblistParam dblistParam = new DblistParam();
            dblistParam.setServiceListId(param.getId());
            dblistParam.setState("0");
            dblistService.alterDbListInfoByServiceId(dblistParam);
        }
        if (param.getEffectiveTime() != null) {
            if (param.getEffectiveTime().indexOf("-") != -1) {
                newEntity.setEffectiveTime((param.getEffectiveTime()));
            } else {
                Long time = Long.parseLong(param.getEffectiveTime());
                newEntity.setEffectiveTime(simpleDateFormat.format(time));
            }
        }

        //判断  如果是 废弃 或者修改密码
        if (param.getState() != null && param.getState() != "") {
            if (param.getState().indexOf("_") != -1) {
                //有下划线说明是废弃功能
                String[] s = param.getState().split("_");
                newEntity.setState(s[0]);
                newEntity.setSpState("3");
                //连接交换机线上数据库
                //TODO 线上没有用户  取消权限功能不能测试
//            String username = param.getUsername();
//            String password = param.getPassword();
//            String dbType = param.getDbType();
//            String driver = "";
//            String url = "";
//            //声明连接
//            Connection conn = null;
//            if (dbType.equals("1")) {
//                //驱动程序名
//                driver = "com.mysql.cj.jdbc.Driver";
//                //URL指向要访问的数据库名mydata
//                url = "jdbc:mysql://" + param.getDbUrl() + ":" + param.getDbNumber() + "/" + param.getDbName() + "?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT";
//            } else if (dbType.equals("2")) {
//                //驱动程序名
//                driver = "oracle.jdbc.OracleDriver";
//                //URL指向要访问的数据库名mydata
//                url = "jdbc:oracle:thin:@" + param.getDbUrl() + ":" + param.getDbNumber() + "/" + param.getDbName();
//            }
//            //遍历查询结果集
//            ResultSet rs = null;
//            try {
//                //加载驱动程序
//                Class.forName(driver);
//                //1.getConnection()方法，连接MySQL数据库！！
//                conn = DriverManager.getConnection(url, username, password);
//                //执行的sql获取该数据库下所有表
//                String sql = "revoke all privileges on " + param.getDbName() + ".* from " + param.getUsername() + "@\"%\"";
//                String sql1 = "flush privileges";
//                Statement stmt = conn.createStatement();
//                stmt.executeUpdate(sql);
//                stmt.executeUpdate(sql1);
//            } catch (SQLException | ClassNotFoundException e) {
//                System.out.println("取消用户权限失败");
//            } finally {
//                try {
//                    rs.close();
//                    conn.close();
//                } catch (SQLException e) {
//                    System.out.println("关闭连接失败");
//                }
//            }
                //数据库废弃之后，管理端将数据库状态为异常
                DblistParam dblistParam = new DblistParam();
                dblistParam.setServiceListId(param.getId());
                dblistParam.setState("0");
                dblistService.alterDbListInfoByServiceId(dblistParam);
            }
        }
        if (param.getPassword() != null && param.getState() != "") {
            if (param.getPassword().indexOf("_") != -1) {
                String[] s = param.getPassword().split("_");
                Integer user = this.baseMapper.findUser(newEntity.getUsername());
                if (user != null && user == 1) {
                    this.baseMapper.editPassword(newEntity.getUsername(), s[0]);
                    this.baseMapper.shuaxin();
                } else {
                    return 500;
                }
            }
        }

        this.updateById(newEntity);
        return 200;
    }

    @Override
    public DbServicelistResult findBySpec(DbServicelistParam param) {
        return null;
    }

    @Override
    public List<DbServicelistResult> findListBySpec(DbServicelistParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DbServicelistParam param) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        Page pageContext = getPageContext();
        param.setUserId(LoginContextHolder.getContext().getUserId());
        if (StringUtils.isEmpty(param.getState())) {
            param.setState("2");
        }

        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<DbServicelistResult> records = page.getRecords();
        for (DbServicelistResult record : records) {
            long time = simpleDateFormat.parse(record.getEffectiveTime()).getTime();
            record.setEffectiveTime(time + "");
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void upDataTime(DbServicelistParam param) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        DbServicelist dbServicelist = baseMapper.selectById(param.getId());
        Date parse = simpleDateFormat.parse(dbServicelist.getEffectiveTime());
        Calendar cd = Calendar.getInstance();
        cd.setTime(parse);
        //增加时间
        if ("3".equals(param.getEffectiveTime())) {
            cd.add(Calendar.MONTH, 3);

        } else if ("6".equals(param.getEffectiveTime())) {
            cd.add(Calendar.MONTH, 6);
        } else if ("9".equals(param.getEffectiveTime())) {
            cd.add(Calendar.YEAR, 1000);
        }
        dbServicelist.setEffectiveTime(simpleDateFormat.format(cd.getTime()));
        dbServicelist.setUpdatedTime(new Date());
        dbServicelist.setUpdatedBy(LoginContextHolder.getContext().getUser().getUsername());
        baseMapper.updateById(dbServicelist);
    }

    @Override
    public LayuiPageInfo findPageBySpecTime(DbServicelistParam param) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        Page pageContext = getPageContext();
        param.setUserId(LoginContextHolder.getContext().getUserId());
        IPage page = this.baseMapper.customPageListTime(pageContext, param);
        List<DbServicelistResult> records = page.getRecords();
        for (DbServicelistResult record : records) {
            long time = simpleDateFormat.parse(record.getEffectiveTime()).getTime();
            record.setEffectiveTime(time + "");
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void offShelf(DbServicelistParam dbServicelistParam) throws ParseException {
//        // 去数据库列表删除记录
//        this.baseMapper.deleteById(dbServicelistParam.getId());
//        // 去数据库详情中的字表删除记录
//        dbServicedetailsService.remove(new QueryWrapper<DbServicedetails>().eq("serviceId", dbServicelistParam.getId()));
//        // 去审核管理把有关申请删除记录
//        // 判断有没有数据库，用户，有就删除数据库，用户，没有就不删
//        Integer database = this.baseMapper.findDatabase(dbServicelistParam.getUsername());
//        Integer user = this.baseMapper.findUser(dbServicelistParam.getUsername());
//        if (user != 0 && database != 0) {
//            this.baseMapper.delDatabase(dbServicelistParam.getUsername());
//            this.baseMapper.delUser(dbServicelistParam.getUsername());
//        }

        DbServicelist entity = this.baseMapper.selectById(dbServicelistParam.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        Date parse = simpleDateFormat.parse(entity.getEffectiveTime());
        Calendar cd = Calendar.getInstance();
        cd.setTime(parse);
        entity.setEffectiveTime(simpleDateFormat.format(cd.getTime()));
        entity.setState("5");//5是等待审核激活
        entity.setSpState("0");
        entity.setUpdatedTime(new Date());
        entity.setUpdatedBy(LoginContextHolder.getContext().getUser().getUsername());
        this.baseMapper.updateById(entity);


        //保存一条通知
        //激活时放一条数据到通知中心
        DbNoticeParam dbNoticeParam = new DbNoticeParam();
        dbNoticeParam.setContent("下架申请");
        dbNoticeParam.setDepartment(entity.getDepartment());
        dbNoticeParam.setSystem(entity.getSystem());
        dbNoticeParam.setSpState("0");
        dbNoticeParam.setState("0");
        dbNoticeParam.setAdminState("0");
        dbNoticeParam.setFirstIn("0");
        dbNoticeParam.setServiceType("1");
        dbNoticeParam.setXxContent(entity.getDbName() + " 数据库申请激活");
        dbNoticeParam.setUserId(String.valueOf(entity.getUserId()));
        dbNoticeParam.setRemark(entity.getStandard());
        dbNoticeParam.setServiceId(entity.getId());
        dbNoticeService.add(dbNoticeParam);
        DbNoticeResult dbNoticeResult = dbNoticeService.findByParam(dbNoticeParam);

        // 下架需要申请  管理端审核通过才能删除
        DbContentapprovalParam dbContentapproval = new DbContentapprovalParam();
        dbContentapproval.setServiceid(entity.getId());
        dbContentapproval.setNoticeId(dbNoticeResult.getId());
        dbContentapproval.setUserId(String.valueOf(entity.getUserId()));
        dbContentapproval.setDepartment(entity.getDepartment());
        dbContentapproval.setContent("3");
        dbContentapproval.setSystem(entity.getSystem());
        dbContentapproval.setServicetype("1");
        dbContentapproval.setStandard(entity.getStandard());
        dbContentapproval.setDetails(entity.getDetails());
        dbContentapproval.setState("0");
        dbContentapproval.setCreatedTime(new Date());
        dbContentapproval.setCreatedBy(LoginContextHolder.getContext().getUser().getUsername());
        dbContentapproval.setUpdatedTime(new Date());
        dbContentapproval.setUpdatedBy(LoginContextHolder.getContext().getUser().getUsername());
        dbContentapprovalService.add(dbContentapproval);
    }

    @Override
    public void active(DbServicelistParam dbServicelistParam, LoginUser user) throws ParseException {
        DbServicelist entity = this.baseMapper.selectById(dbServicelistParam.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        Date parse = simpleDateFormat.parse(entity.getEffectiveTime());
        Calendar cd = Calendar.getInstance();
        cd.setTime(parse);
        //增加时间
        if ("3".equals(dbServicelistParam.getEffectiveTime())) {
            cd.add(Calendar.MONTH, 3);

        } else if ("6".equals(dbServicelistParam.getEffectiveTime())) {
            cd.add(Calendar.MONTH, 6);
        } else if ("9".equals(dbServicelistParam.getEffectiveTime())) {
            cd.add(Calendar.YEAR, 1000);
        }

        entity.setEffectiveTime(simpleDateFormat.format(cd.getTime()));
        entity.setState("5");//5是等待审核激活
        entity.setSpState("0");
        entity.setUpdatedTime(new Date());
        entity.setUpdatedBy(user.getUsername());
        this.baseMapper.updateById(entity);

        //激活时放一条数据到通知中心
        DbNoticeParam dbNoticeParam = new DbNoticeParam();
        dbNoticeParam.setContent("激活申请");
        dbNoticeParam.setDepartment(entity.getDepartment());
        dbNoticeParam.setSystem(entity.getSystem());
        dbNoticeParam.setSpState("0");
        dbNoticeParam.setState("0");
        dbNoticeParam.setAdminState("0");
        dbNoticeParam.setFirstIn("0");
        dbNoticeParam.setServiceType("1");
        dbNoticeParam.setXxContent(entity.getDbName() + " 数据库申请激活");
        dbNoticeParam.setUserId(String.valueOf(user.getId()));
        dbNoticeParam.setRemark(entity.getStandard());
        dbNoticeParam.setServiceId(entity.getId());
        dbNoticeService.add(dbNoticeParam);
        DbNoticeResult dbNoticeResult = dbNoticeService.findByParam(dbNoticeParam);

        //保存到审核表
        DbContentapproval dbContentapproval = new DbContentapproval();
        BeanUtils.copyProperties(entity, dbContentapproval);
        dbContentapproval.setServiceid(entity.getId());
        dbContentapproval.setNoticeId(dbNoticeResult.getId());
        dbContentapproval.setUserId(user.getId() + "");
        dbContentapproval.setContent("5");//5是激活申请
        dbContentapproval.setRemarks(entity.getRemarks());
        dbContentapproval.setState("0");//0是默认待处理
        dbContentapprovalService.save(dbContentapproval);
    }

    @Override
    public LayuiPageInfo findServicePageBySpec(DbServicelistParam param) throws ParseException {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageServiceList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo queryApply(DbServicelistParam param) throws ParseException {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.queryApplyList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public String testConnectServer(DbServicelistParam param) {
        //测试连接服务器返回数据clear
        String clear = "";
        String ipAddr = param.getDbUrl();

        try {
            InetAddress address = InetAddress.getByName(ipAddr);
            if (address instanceof java.net.Inet4Address) {
                System.out.println(ipAddr + " is ipv4 address");
            } else if (address instanceof java.net.Inet6Address) {
                System.out.println(ipAddr + " is ipv6 address");
            } else {
                System.out.println(ipAddr + " is unrecongized");
            }
            //使用isReachable(int timeout)判断是否能够ping通
            if (address.isReachable(3000)) {
                clear = "1";
            } else {
                clear = "2";
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }


        //TODO  Oracle数据库连接不知道是否正确
        //数据库返回值
        String returnString = "";
        //声明Connection对象
        Connection con;
        //数据库配置时的用户名
        String user = param.getUsername();
        //数据库配置时的密码
        String password = param.getPassword();
        String driver = "";
        String url = "";
        if (param.getDbType().equals("1")) {
            //驱动程序名
            driver = "com.mysql.cj.jdbc.Driver";
            //URL指向要访问的数据库名mydata
            url = "jdbc:mysql://" + param.getDbUrl() + ":" + param.getDbNumber() + "/" + param.getDbName() + "?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT";
        } else if (param.getDbType().equals("2")) {
            //驱动程序名
            driver = "oracle.jdbc.OracleDriver";
            //URL指向要访问的数据库名mydata
            url = "jdbc:oracle:thin:@" + param.getDbUrl() + ":" + param.getDbNumber() + "/" + param.getDbName();
        }
        //遍历查询结果集
        try {
            //加载驱动程序
            Class.forName(driver);
            //1.getConnection()方法，连接MySQL数据库！！
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                returnString = "1";
            }
            con.close();
        } catch (ClassNotFoundException e) {
            //数据库驱动类异常处理
            returnString = "2";
//            e.printStackTrace();
        } catch (SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();
            returnString = "2";
        } catch (Exception e) {
            e.printStackTrace();
            returnString = "2";
        } finally {
        }
        return clear + "," + returnString;
    }

    @Override
    public List<Exchange> searchExchangeList(Exchange exchange) {
        return this.baseMapper.queryExchangeListInfo(exchange);
    }

    @Override
    public DbServicelistResult queryDbServiceListInfo(Integer id) {
        return this.baseMapper.queryDbServiceListById(id);
    }

    @Override
    public LayuiPageInfo queryServiceListByParam(DbServicelistParam param) throws ParseException {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.queryServiceList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }


    private Serializable getKey(DbServicelistParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DbServicelist getOldEntity(DbServicelistParam param) {
        return this.getById(getKey(param));
    }

    private DbServicelist getEntity(DbServicelistParam param) {
        DbServicelist entity = new DbServicelist();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
